<!--
 * @Author: lh
 * @Date: 2023-07-11 13:52:31
 * @LastEditors: lh
 * @LastEditTime: 2023-10-20 17:56:02
-->
<template>
  <div>
    <!-- :searchDataList="searchDataListJy" -->
    <!-- <personnel></personnel> -->
    <tabCom
      @Refresh="Refresh"
      @search-callback="searchCallback"
      :title="'设计文件接收/著录/导入'"
      :operation-api="operationApiA"
      :table-data-rule="tableDataRuleA"
      :search-data-list="searchDataListJy"
      :needPagination="true"
      :update_show_key="'id'"
      :update_show_value="[0]"
      :formDataWidth="'30%'"
      :showDownloadTem="true"
      :showUpload="true"
      :isAddAjaxData="true"
      :addAjaxData="{ supervisionResponsiblePerson: userId }"
      :addButtonList="addButtonList"
      @buttonMoudle="buttonMoudle"
      ref="tabCombox"
    >
      <template #add="{ formData }">
        <el-form-item :label="item.title" v-for="(item, index) in searchDataListJyEdit" :key="index">
          <el-input v-if="!item.type" v-model="formData[item.name]" placeholder="请输入" />
          <personnel v-model="formData[item.name]" v-else-if="item.type == 'user'"></personnel>
          <personnelMore v-model="formData[item.name]" v-else-if="item.type == 'userMore'"></personnelMore>
          <el-input
            v-else-if="item.type == 'number'"
            type="number"
            v-model="formData[item.name]"
            placeholder="请输入"
          />
          <!-- value-format="YYYY/MM/DD" -->
          <el-date-picker
            v-else-if="item.type == 'timeday'"
            v-model="formData[item.name]"
            type="date"
            format="YYYY/MM/DD"
            placeholder="请选择"
          />
          <el-date-picker
            v-else-if="item.type == 'datetime'"
            v-model="formData[item.name]"
            type="datetime"
            format="YYYY/MM/DD HH:mm:ss"
            placeholder="请选择"
          />
          <upfilebox
            v-else-if="item.type == 'file'"
            fileType="file"
            v-model="formData[item.name]"
            v-model:name="formData[item.prop]"
          ></upfilebox>
          <el-date-picker
            @change="chooseTimeChange(formData[item.name])"
            v-else-if="item.type == 'time'"
            v-model="formData[item.name]"
            type="datetimerange"
            format="YYYY/MM/DD"
            value-format="YYYYMMDD"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
          <el-select
            v-else-if="item.type == 'select' && item!.list!.length > 0"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            allow-create
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.value || items.id"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'selectLable'"
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
          <el-select
            v-else-if="item.type == 'multipleSelect'"
            multiple
            v-model="formData[item.name]"
            default-first-option
            :reserve-keyword="false"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label || items.name"
              :value="items.label || items.name"
            />
          </el-select>
        </el-form-item>
      </template>
      <template #examinationState="{ data }">
        <div style="width: 100%">
          <el-steps :active="data.lettersState" :align-center="true" finish-status="success">
            <el-step title="总监审核" />
            <el-step title="部门审核" />
            <el-step title="主办人审核" />
          </el-steps>
        </div>
      </template>
    </tabCom>
    <!-- 启动审查 -->
    <el-dialog v-model="dialogVisible" :title="'启动审查'" @close="resetForm" width="30%">
      <el-form ref="formRef" label-width="100px" label-position="left">
        <el-form-item prop="actionItemEvolve" label="审查相关人">
          <personnelMore v-model="actionItemEvolve"></personnelMore>
        </el-form-item>
        <el-form-item v-if="tablerow.isCountersign == 1" prop="professionalSupervisionEngineerUids" label="会审相关人">
          <personnelMore v-model="professionalSupervisionEngineerUids"></personnelMore>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreate">确认</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import tabCom from "@/components/table/indexcopy.vue"
// import { gethomeStatistics } from "@/api/home/index"
import { ref, onMounted } from "vue"
import upfilebox from "@/components/table/upfile.vue"
import { useUserStoreHook } from "@/store/modules/user"
import { ElMessage, ElMessageBox } from "element-plus"
import { userInitiateReviewProcess } from "@/api/procedure"

const { userId } = useUserStoreHook()
const tabCombox = ref<typeof tabCom>()

onMounted(() => {
  init()
})
// 接口请求配置
const operationApiA = ref<any>([
  { type: "add", interface: "/designFile/insertOrUpdate" },
  { type: "update", interface: "/designFile/insertOrUpdate" },
  { type: "search", interface: "/designFile/selectAll" },
  { type: "delete", interface: "/designFile/deleteByIds" },
  { type: "svg", interface: "/designFile/export" },
  { type: "uploadFile", interface: "/designFile/importData" },
  { type: "downloadTem", interface: "/designFile/downloadTemplate" }
])
//#region  添加按钮所有逻辑
// 添加按钮
const addButtonList = [
  // { name: "启动审查", type: "infill" }
]
const tablerow = ref() // 当前操作表单数据
// 按钮回调
const buttonMoudle = (type: string, row: any, Face: string) => {
  tablerow.value = row
  console.log(type, row, "-*-buttonMoudle")
  switch (type) {
    case "infill": // 启动审查
      infillFn()
      break
  }
}
let dialogVisible = ref(false) // 启动审查弹框
let actionItemEvolve = ref("") // 审查相关人(各部门负责人用户ids(多个用英文逗号隔开)--审查)
let professionalSupervisionEngineerUids = ref("") // 会审部门审查
// 填写反馈行动项进展 方法
const infillFn = () => {
  console.log("infillFn", userId)
  // 当前还没验证
  if (tablerow.value.examinationState == 0) {
    // if (userId == tablerow.value.relevanceUserIds) {
    dialogVisible.value = true
    // } else {
    //   ElMessage.error("你不是该关联人~")
    // }
  } else {
    ElMessage({
      message: "已操作",
      type: "warning"
    })
  }
}
// 重置数据
const resetForm = () => {
  actionItemEvolve.value = ""
  professionalSupervisionEngineerUids.value = ""
}
// 确认反馈
const handleCreate = () => {
  if (!actionItemEvolve.value) {
    ElMessage.error("请选择审查相关人")
    return
  }
  userInitiateReviewProcess({
    departmentHeadsUids: actionItemEvolve.value,
    id: tablerow.value.id,
    professionalSupervisionEngineerUids: professionalSupervisionEngineerUids.value
  }).then((res) => {
    console.log(res, "---res")

    if (res.code == 200) {
      ElMessage({ message: "提交成功", type: "success" })
      tabCombox.value.resetSearch()
      dialogVisible.value = false
      resetForm()
    }
  })
}

//#endregion

// 搜索配置
const searchDataListJy = [
  { name: "id", title: "id" },
  { name: "designFileNum", title: "设计文件编码" },
  { name: "projectName", title: "项目名称" },
  { name: "atlasNum", title: "图册编号" },
  { name: "designFileName", title: "设计文件名称" },
  { name: "fileVersion", title: "版本" },
  {
    name: "fileState",
    title: "状态",
    type: "select",
    list: [
      { value: "PRE", label: "PRE", name: "PRE", id: 0 },
      { value: "CFC", label: "CFC", name: "CFC", id: 1 },
      { value: "过时", label: "过时", name: "过时", id: 1 },
      { value: "作废", label: "作废", name: "作废", id: 1 }
    ]
  },

  {
    name: "fileType",
    title: "文件类型",
    type: "select",
    list: [
      { value: "图纸", label: "图纸", name: "图纸", id: 0 },
      { value: "技术规格书", label: "技术规格书", name: "技术规格书", id: 1 },
      { value: "设备竣工文件", label: "设备竣工文件", name: "设备竣工文件", id: 2 },
      { value: "CR", label: "CR", name: "CR", id: 3 },
      { value: "FCR", label: "FCR", name: "FCR", id: 4 },
      { value: "DEN", label: "DEN", name: "DEN", id: 5 }
    ]
  },
  {
    name: "readingState",
    title: "阅图状态",
    type: "select",
    list: [
      { value: "未阅", label: "未阅", name: "未阅", id: 0 },
      { value: "部分未阅", label: "部分未阅", name: "部分未阅", id: 1 },
      { value: "已阅无意见", label: "已阅无意见", name: "已阅无意见", id: 2 },
      { value: "已阅有意见", label: "已阅有意见", name: "已阅有意见", id: 3 }
    ]
  },
  {
    name: "distributionState",
    title: "分发状态",
    type: "select",
    list: [
      { value: "待分发", label: "待分发", name: "待分发", id: 0 },
      { value: "已分发", label: "已分发", name: "已分发", id: 1 }
    ]
  },
  { name: "startTime", title: "开始时间", type: "datetime" },
  { name: "endTime", title: "结束时间", type: "datetime" }
]
// 添加配置
const searchDataListJyEdit = [
  { name: "designFileNum", title: "设计文件编码" },
  { name: "projectName", title: "项目名称" },
  { name: "atlasNum", title: "图册编号" },
  { name: "designFileName", title: "设计文件名称" },
  { name: "fileVersion", title: "版本" },
  {
    name: "fileState",
    title: "状态",
    type: "select",
    list: [
      { value: "PRE", label: "PRE", name: "PRE", id: 0 },
      { value: "CFC", label: "CFC", name: "CFC", id: 1 },
      { value: "过时", label: "过时", name: "过时", id: 1 },
      { value: "作废", label: "作废", name: "作废", id: 1 }
    ]
  },
  { name: "sharedDepartment", title: "共享部门" },
  { name: "position", title: "存放位置" },
  { name: "organizationUnit", title: "编制单位" },
  { name: "organizationTime", title: "编制日期", type: "datetime" },
  {
    name: "fileType",
    title: "文件类型",
    type: "select",
    list: [
      { value: "图纸", label: "图纸", name: "图纸", id: 0 },
      { value: "技术规格书", label: "技术规格书", name: "技术规格书", id: 1 },
      { value: "设备竣工文件", label: "设备竣工文件", name: "设备竣工文件", id: 2 },
      { value: "CR", label: "CR", name: "CR", id: 3 },
      { value: "FCR", label: "FCR", name: "FCR", id: 4 },
      { value: "DEN", label: "DEN", name: "DEN", id: 5 }
    ]
  },
  { name: "fileUrl", title: "附件文件", type: "file", prop: "fileName" },
  // { name: "supervisionResponsiblePerson", title: "监理责任人" },
  { name: "readingTimeLimit", title: "阅图时间限制(单位分钟)", type: "number" },
  { name: "dispatchesTime", title: "收文日期", type: "datetime" },
  { name: "remarks", title: "备注" }
]
// 表单配置
const tableDataRuleA = ref([
  { prop: "id", label: "id" },
  { prop: "designFileNum", label: "设计文件编码" },
  { prop: "projectName", label: "项目名称" },
  { prop: "atlasNum", label: "图册编号" },
  { prop: "designFileName", label: "设计文件名称" },
  { prop: "fileVersion", label: "版本" },
  { prop: "fileState", label: "状态" },
  { prop: "sharedDepartment", label: "共享部门" },
  { prop: "position", label: "存放位置" },
  { prop: "organizationUnit", label: "编制单位" },
  { prop: "organizationTime", label: "编制日期", type: "time" },
  { prop: "fileType", label: "文件类型" },
  { prop: "supervisionResponsiblePersonName", label: "监理责任人" },
  { prop: "responsiblePersonCollectionTime", label: "责任人收图日期" },
  { prop: "filename", label: "附件文件", type: "fileUrl", assoc: "fileName" },
  { prop: "readingTimeLimit", label: "阅图时间限制(单位分钟)" },
  { prop: "readingState", label: "阅图状态" },
  { prop: "dispatchesTime", label: "收文日期", type: "time" },
  { prop: "distributionState", label: "分发状态" },
  { prop: "remarks", label: "备注" }
  // {prop: 'operate',label: '操作'},
])
// 初始化
const init = () => {}
// 表单刷新
const Refresh = () => {
  //用户数据概览
}
// 搜索回调
const searchCallback = (search: any) => {}
const loading = ref<boolean>(false)
const gailan = ref<any>({})
const gethomeStatisticsStar = (obj: any = {}) => {
  // loading.value = true
  // gethomeStatistics(obj).then((res: any) => {
  //   gailan.value = res.data
  //   loading.value = false
  // })
}

// 时间选择限定范围
const chooseTimeChange = (time: any) => {
  const starTime = JSON.parse(JSON.stringify(time[0]))
  const endTime = JSON.parse(JSON.stringify(time[1]))
  const minTime = new Date(insertStr(insertStr(starTime, 4, "-"), 7, "-")).getTime()
  const maxTime = new Date(insertStr(insertStr(endTime, 4, "-"), 7, "-")).getTime()
  const allow = 2592000000 //30天
  if (maxTime - minTime > allow) {
    ElMessage.warning("选择范围请在30天以内!")
    chaxunState.value = false
  } else {
    chaxunState.value = true
  }
}
</script>
<style scoped lang="scss">
.search-wrapper {
  margin: 0 auto;
  width: 98.5%;
  margin-top: 10px;
}
</style>
